<template>
  <div class="xtx-pay-page" v-if="orderInfo">
    <div class="container">
      <xtx-bread>
        <xtx-bread-item to="/">首页</xtx-bread-item>
        <xtx-bread-item to="/cart">购物车</xtx-bread-item>
        <xtx-bread-item>支付订单</xtx-bread-item>
      </xtx-bread>
      <!-- 付款信息 -->
      <div class="pay-info">
        <!-- 订单30分钟的有效期，后端返回cutdown选项，为倒计时的秒数，当为-1代表已过期 -->
        <!-- 订单有效显示绿色√ -->
        <span v-if="orderInfo && orderInfo.countdown !== -1" class="icon iconfont icon-queren2"></span>
        <!-- 订单失效显示红色× -->
        <span v-if="orderInfo?.countdown === -1" class="icon iconfont icon-shanchu red"></span>
        <!-- 订单在有效期内，显示倒计时时间 -->
        <div class="tip" v-if="orderInfo?.countdown !== -1">
          <p>订单提交成功！请尽快完成支付。</p>
          <p>支付还剩 <span>{{ orderInfo ? timeText : '30分钟' }}</span>, 超时后将取消订单</p>
        </div>
        <!-- 订单已经失效的处理 -->
        <div class="tip" v-else>
          <p>订单已失效！请重新选择商品进行支付。</p>
          <p style="margin-top: 20px;">
            <RouterLink to="/">
              <xtx-button type="primary">去逛逛</xtx-button>
            </RouterLink>
          </p>
        </div>
        <div class="amount">
          <span>应付总额：</span>
          <span>¥{{ orderInfo.payMoney.toFixed(2) }}</span>
        </div>
      </div>
      <!-- 付款方式 -->
      <div class="pay-type">
        <p class="head">选择以下支付方式付款</p>
        <div class="item">
          <p>支付平台</p>
          <a :class="{ active: orderInfo.payType === 2 }" class="btn wx" href="javascript:;"></a>
          <a @click="toAlipay" :class="{ active: orderInfo.payType === 1 }" class="btn alipay" href="javascript:;"></a>
        </div>
        <div class="item">
          <p>支付方式</p>
          <a class="btn" href="javascript:;">招商银行</a>
          <a class="btn" href="javascript:;">工商银行</a>
          <a class="btn" href="javascript:;">建设银行</a>
          <a class="btn" href="javascript:;">农业银行</a>
          <a class="btn" href="javascript:;">交通银行</a>
        </div>
      </div>
    </div>
  </div>
  <xtx-dialog title="正在支付..." v-model:visible="visibleDialog">
    <div class="pay-wait">
      <img src="@/assets/images/load.gif" alt="">
      <div v-if="order">
        <p>如果支付成功：</p>
        <RouterLink :to="`/member/order/${order.id}`">查看订单详情></RouterLink>
        <p>如果支付失败：</p>
        <RouterLink to="/">查看相关疑问></RouterLink>
      </div>
    </div>
  </xtx-dialog>
</template>
<script setup>
import { ref, watch } from 'vue'
import { findOrderDetailApi } from '@/api/order'
import { useRoute } from 'vue-router'
import { baseURL } from '@/utils/request'
import { alipayRedirectUrl } from '@/config'
// 倒计时处理函数
import { usePayTime } from '@/hook'
const route = useRoute()
const orderInfo = ref(null)
const { start, timeText } = usePayTime()
// 侦听订单id的变化
watch(() => route.query.id, orderId => {
  if (orderId && route.path === '/member/pay') {
    findOrderDetailApi(orderId).then(data => {
      orderInfo.value = data.result
      // 开启定时器
      if (data.result.countdown !== -1) {
        // 开启定时器
        start(data.result.countdown)
      }
    })
  }
}, { immediate: true })

// 在支付成功回调回来前，给一个正在支付的对话框
const visibleDialog = ref(false)

// 去支付宝支付
function toAlipay() {
  // 打开对话框
  visibleDialog.value = true
  // 回调地址
  const redirect = encodeURIComponent(alipayRedirectUrl)
  // 跳转到后端的支付页面
  location.href = baseURL + `/pay/aliPay?redirect=${redirect}&orderId=${route.query.id}`
}
</script>
<style scoped lang="less">
// 支付等待通知的样式
.pay-wait {
  display: flex;
  justify-content: space-around;

  p {
    margin-top: 30px;
    font-size: 14px;
  }

  a {
    color: @xtxColor;
  }
}

.pay-info {
  background: #fff;
  display: flex;
  align-items: center;
  height: 240px;
  padding: 0 80px;

  .icon {
    font-size: 80px;
    color: #1dc779;
  }

  .tip {
    padding-left: 10px;
    flex: 1;

    p {
      &:first-child {
        font-size: 20px;
        margin-bottom: 5px;
      }

      &:last-child {
        color: #999;
        font-size: 16px;
      }
    }
  }

  .amount {
    span {
      &:first-child {
        font-size: 16px;
        color: #999;
      }

      &:last-child {
        color: @priceColor;
        font-size: 20px;
      }
    }
  }
}

.pay-type {
  margin-top: 20px;
  background-color: #fff;
  padding-bottom: 70px;

  p {
    line-height: 70px;
    height: 70px;
    padding-left: 30px;
    font-size: 16px;

    &.head {
      border-bottom: 1px solid #f5f5f5;
    }
  }

  .btn {
    width: 150px;
    height: 50px;
    border: 1px solid #e4e4e4;
    text-align: center;
    line-height: 48px;
    margin-left: 30px;
    color: #666666;
    display: inline-block;

    &.active,
    &:hover {
      border-color: @xtxColor;
    }

    &.alipay {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/7b6b02396368c9314528c0bbd85a2e06.png) no-repeat center / contain;
    }

    &.wx {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66f98cff8649bd5ba722c2e8067c6ca.jpg) no-repeat center / contain;
    }
  }
}</style>